package com.gitee.kamismile.stone.basic.data.starter;

import com.gitee.kamismile.stone.basic.data.infrastructure.repository.handler.r2.convert.DateConverter;
import com.gitee.kamismile.stone.basic.data.infrastructure.repository.handler.r2.convert.JsonObjectConverter;
import com.gitee.kamismile.stone.basic.data.infrastructure.repository.handler.r2.mapping.CustomMappingR2dbcConverter;
import com.gitee.kamismile.stone.commmon.vertx.ServiceVertxConfigProperties;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.r2dbc.convert.MappingR2dbcConverter;
import org.springframework.data.r2dbc.convert.R2dbcCustomConversions;
import org.springframework.data.r2dbc.dialect.MySqlDialect;
import org.springframework.data.r2dbc.mapping.R2dbcMappingContext;
import org.springframework.data.r2dbc.repository.config.EnableR2dbcRepositories;
import org.springframework.util.Assert;

@Configuration
@EnableR2dbcRepositories(basePackages = "com.gitee.kamismile.stone.basic.data.infrastructure.repository.r2Repository.*.repository")
@EntityScan(basePackages = "com.gitee.kamismile.stone.basic.data.domain.*.repository.entity")
@ComponentScan(basePackages = "com.gitee.kamismile.stone.basic.data")
public class BasicDataSpringConfiguration {
    @Bean
    public R2dbcCustomConversions r2dbcCustomConversions(){

        return R2dbcCustomConversions.of(MySqlDialect.INSTANCE,
                new JsonObjectConverter(),
                new JsonObjectConverter.JsonObjectToStringConverter(),
                new DateConverter(),
                new DateConverter.DateToZonedDateTimeConverter()
        );
    }

    @Bean
    public MappingR2dbcConverter r2dbcConverter(R2dbcMappingContext mappingContext,
                                                R2dbcCustomConversions r2dbcCustomConversions) {

        Assert.notNull(mappingContext, "MappingContext must not be null");

        return new CustomMappingR2dbcConverter(mappingContext, r2dbcCustomConversions);
    }

}
